package com.material.vtool.service.impl;

import com.material.vtool.entity.BTeamInfoEntity;
import com.material.vtool.entity.BTeamRelationEntity;
import com.material.vtool.entity.combinedEntity.CombinedTeamMemberEntity;
import com.material.vtool.entity.combinedEntity.UserTeamEntity;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.exception.ServiceException;
import com.material.vtool.mapper.TeamRelationMapper;
import com.material.vtool.service.TeamRelationService;
import com.material.vtool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeamRelationServiceImpl implements TeamRelationService {
    @Autowired
    private TeamRelationMapper teamRelationMapper;

    @Override
    @Transactional
    public int saveTeamRelation(BTeamRelationEntity bTeamRelationEntity) {
        // 1、新增团队成员
        Integer affectedCount = teamRelationMapper.insertTeamRelation(bTeamRelationEntity);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);

        // 2、更新团队成员的其他团队为不活跃状态
        teamRelationMapper.updateUserFlag(bTeamRelationEntity.getUserId(), bTeamRelationEntity.getTeamId());

        return affectedCount.intValue();
    }

    @Override
    @Transactional
    public int updateTeamMember(BTeamRelationEntity bTeamRelationEntity) {
        // 1、更新团队成员的信息
        Integer affectedCount = teamRelationMapper.updateTeamMember(bTeamRelationEntity);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("更新失败", ReturnCode.SERVER_ERROR);

        // 2、更新团队成员的其他团队为不活跃状态
        teamRelationMapper.updateUserFlag(bTeamRelationEntity.getUserId(), bTeamRelationEntity.getTeamId());

        return affectedCount.intValue();
    }

    @Override
    public BTeamRelationEntity getTeamMember(Integer userId, Integer teamId) {
        return teamRelationMapper.getTeamMember(userId, teamId);
    }

    @Override
    public List<CombinedTeamMemberEntity> getTeamMemberListByKey(Integer teamId, String key) {
        return teamRelationMapper.getTeamMemberListByKey(teamId, key);
    }

    @Override
    public int getTeamMemberByKeyCount(Integer teamId, String key) {
        Integer affectedCount = teamRelationMapper.getTeamMemberByKeyCount(teamId, key);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        return affectedCount.intValue();
    }

    @Override
    public List<CombinedTeamMemberEntity> getTeamMemberList(Integer teamId) {
        return teamRelationMapper.getTeamMemberList(teamId);
    }

    @Override
    public int getTeamMemberCount(Integer teamId) {
        Integer affectedCount = teamRelationMapper.getTeamMemberCount(teamId);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("保存失败", ReturnCode.SERVER_ERROR);
        return affectedCount.intValue();
    }

    @Override
    public List<UserTeamEntity> getUserTeamList(Integer userId) {
        return teamRelationMapper.getUserTeamList(userId);
    }

    @Override
    public BTeamInfoEntity getUserActiveTeamInfo(Integer userId) {
        return teamRelationMapper.getUserActiveTeamInfo(userId);
    }

    @Override
    @Transactional
    public int updateActiveTeam(Integer userId, Integer teamId) {
        // 1、设置活跃团队
        BTeamRelationEntity bTeamRelationEntity = new BTeamRelationEntity();
        bTeamRelationEntity.setUserId(userId);
        bTeamRelationEntity.setTeamId(teamId);
        bTeamRelationEntity.setUserFlag(Integer.valueOf(1));
        bTeamRelationEntity.setModifyTime(DateUtil.getCurDate());
        Integer affectedCount = teamRelationMapper.updateTeamMember(bTeamRelationEntity);
        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("更新失败", ReturnCode.SERVER_ERROR);

        // 2、更新团队成员的其他团队为不活跃状态
        teamRelationMapper.updateUserFlag(userId, teamId);
        return affectedCount.intValue();
    }

    @Override
    @Transactional
    public int exitTeam(Integer userId, Integer teamId) {
        BTeamRelationEntity bTeamRelationEntity = new BTeamRelationEntity();
        bTeamRelationEntity.setUserId(userId);
        bTeamRelationEntity.setTeamId(teamId);
        bTeamRelationEntity.setUserTeamStatus(0);
        bTeamRelationEntity.setUserFlag(Integer.valueOf(0));// 退出后，设置为不活跃状态
        bTeamRelationEntity.setModifyTime(DateUtil.getCurDate());
        Integer affectedCount = teamRelationMapper.updateTeamMember(bTeamRelationEntity);

        if (affectedCount == null || affectedCount.intValue() == 0) throw new ServiceException("更新失败", ReturnCode.SERVER_ERROR);

        // 获取团队成员所在的其他团队，并且选择最新的
        Integer id = teamRelationMapper.getMemberLatestTeam(userId, teamId);
        if (id != null && id.intValue() > 0) {
            // 如果存在，则把该团队设为该用户的活跃团队
            teamRelationMapper.updateMemberLatestTeamActive(id);
        }

        return affectedCount.intValue();
    }
}
